
<html>
<head><title>Textarea scrolling</title></head>
<body bgcolor=yellow style=margin-left:.4in>
<form action='' method=get>
<textarea id=ta name=ta>
This is some initial
text for the purpose of
searching in the textarea
to see how scrolling to a
non currently showing section works
</textarea><br>
Sear<u>c</u>h term: <input type=text id=tbNeedle
name=tbNeedle accesskey=c>
<button type=button accessKey=s
onclick="search(elem('tbNeedle').value,'ta')"[color=blue]
>Te<u>s</u>t</button>[/color]
</form>
Suggestion:<br>
(1) Don't put focus in the textarea<br>
(2) First search for 'the textarea' (without quotes)<br>
(3) Then search for 'This'
<script type='text/javascript'>
function elem(id) { return document.getElementById(id); }
function search(needle,haystack,start) {
var i, element = elem(haystack);
index = element.value.indexOf(needle,start||0);
if (index<0) {
window.status = "Not found: " + needle;
window.setTimeout (function(){window.status=''}, 4000); }
else {
element.setSelectionRange((i=index+needle.length)-1,i);
var ev = document.createEvent ('KeyEvents');
ev.initKeyEvent('keypress', true, true, window,
false, false, false, false, 0,
element.value.charCodeAt(i-1));
element.dispatchEvent(ev); // causes the scrolling
element.setSelectionRange(index, i);
}
}
</script>
</body></html>